//
//  HTMDIViewController.m
//  HTERPManApp
//
//  Created by standychen on 14-11-10.
//  Copyright (c) 2014年 HT. All rights reserved.
//

#import "htvmdi.h"
#import "HTFrameKit.h"
#import "CHTumblrMenuView.h"
#import "HTSysKit.h"
#import "RNGridMenu.h"
#import "AppDelegate.h"



#define theEndTag 255
#define timeout 50

@interface htvmdi (){
    NSMutableArray *soList;
    RNGridMenu *av ;

}

@end

@implementation htvmdi

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    
    
    return self;
}

//启动MDI执行方法
- (void)viewDidLoad
{
    [super viewDidLoad];
    
//    [Singleton sharedInstance].socketHost = @"www.icoix.com";// host设定
//    [Singleton sharedInstance].socketPort = 3000;// port设定
//    
//    // 在连接前先进行手动断开
//    [Singleton sharedInstance].socket.userData = SocketOfflineByUser;
//    [[Singleton sharedInstance] cutOffSocket];
//    
//    // 确保断开后再连，如果对一个正处于连接状态的socket进行连接，会出现崩溃
//    [Singleton sharedInstance].socket.userData = SocketOfflineByServer;
//    [[Singleton sharedInstance] socketConnectHost];
    
    
    soList=[NSMutableArray array];
    
    //如果自动，访问服务器进行数据校验
    if([HTSysParam autoLoginFlag])
    {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSString *company = [defaults objectForKey:@"comany"];
        NSString *username = [defaults objectForKey:@"username"];
        NSString *userpassword = [defaults objectForKey:@"password"];
        
        [HTSysLogin hmCheckLogin_ASI:company andUserName:username andPassword:userpassword complete:^{
            
        } finally:^{
            
        }];
    }
    

    
    UIColor *tintColor=[UIColor colorWithRed:91.0/255 green:138.0/255 blue:218.0/255 alpha:1.0f];
    
    
    UIStoryboard *soStory = [UIStoryboard storyboardWithName:@"FirstPage" bundle:nil];
    UIViewController *vc1 = [soStory instantiateInitialViewController];
    vc1.tabBarItem.title=@"发现";
    UIImage *img1 = [UIImage imageNamed:@"4"];
    img1 =  [img1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [vc1.tabBarItem setFinishedSelectedImage:img1
                 withFinishedUnselectedImage:[UIImage imageNamed:@"1"]];
    [vc1.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                            tintColor, UITextAttributeTextColor,
                                            nil] forState:UIControlStateSelected];
    
    
    UIStoryboard *productStory = [UIStoryboard storyboardWithName:@"SampleForm" bundle:nil];
    UIViewController *vc3 = [productStory instantiateInitialViewController];
    vc3.tabBarItem.title=@"选样";
    UIImage *img3 = [UIImage imageNamed:@"5"];
    img3 =  [img3 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [vc3.tabBarItem setFinishedSelectedImage:img3
                 withFinishedUnselectedImage:[UIImage imageNamed:@"2"]];
    [vc3.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                            tintColor, UITextAttributeTextColor,
                                            nil] forState:UIControlStateSelected];
    
    
    UIStoryboard *foStory = [UIStoryboard storyboardWithName:@"MeForm" bundle:nil];
    UIViewController *vc2 = [foStory instantiateInitialViewController];
    vc2.tabBarItem.title=@"我的";
    UIImage *img2 = [UIImage imageNamed:@"6"];
    img2 =  [img2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [vc2.tabBarItem setFinishedSelectedImage:img2
                 withFinishedUnselectedImage:[UIImage imageNamed:@"3"]];
    [vc2.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                            tintColor, UITextAttributeTextColor,
                                            nil] forState:UIControlStateSelected];

    self.viewControllers= @[vc1,vc3,vc2];//添加到导航页
    
    
}



-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
{
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
    button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
    [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
    
    [button addTarget:self action:@selector(centerClicked)forControlEvents:UIControlEventTouchUpInside];
    
    CGFloat heightDifference = buttonImage.size.height - self.tabBar.frame.size.height;
    if (heightDifference < 0)
        button.center = self.tabBar.center;
    else
    {
        CGPoint center = self.tabBar.center;
        center.y = center.y - heightDifference/2.0;
        button.center = center;
    }
    
    
    [self.view addSubview:button];
}

//刷新数据
-(void)refreshListData
{
    NSString *serverUrl=[[NSString alloc]initWithFormat:@"%@\/%@\/",[HTSysParam erpServer],[HTSysParam erpVirDir]];
    HTFrameNet *htnet=[[HTFrameNet alloc] initWithServerUrl:serverUrl];
    
    
    
    NSDictionary *dict = [[NSDictionary alloc]init];
    [htnet hmNetWCFmethodName:@"srvcreateform/QryCreateTable" andDic:dict complete:^(NSString *backstr)
     {
         NSDictionary *rootDict=[HTFrameNet hmConvertDictFromJsonStr:backstr];
         NSMutableArray *resultdt=[rootDict objectForKey:kpNET_RETURN_ENTITY_DATA];//[HTFrameNet hmConvertArrayFromJsonStr:backstr];
         
         for (int i=0; i<[resultdt count]; i++) {
             [soList addObject:[resultdt objectAtIndex:i]];
             
         }
         
         
         
     }];
    
    
    
    
}

-(void)centerClicked{
    
    [self showGrid];

}



- (void)showGrid {
    
    NSInteger numberOfOptions = [soList count];
    NSMutableArray *items=[NSMutableArray array];
    for(int i=0;i<[soList count];i++){
        NSDictionary * dict = [soList objectAtIndex:i];
        NSString *title=[dict objectForKey:@"Title"];
        NSString *icons=[dict objectForKey:@"ICon"];
        [items addObject:[[RNGridMenuItem alloc] initWithImage:[UIImage imageNamed:icons] title:title]];
    }
    
    
    av = [[RNGridMenu alloc] initWithItems:[items subarrayWithRange:NSMakeRange(0, numberOfOptions)]];
    av.delegate = self;
    //    av.bounces = NO;
    [av showInViewController:self center:CGPointMake(self.view.bounds.size.width/2.f, self.view.bounds.size.height/2.f)];
}

#pragma mark - RNGridMenuDelegate

- (void)gridMenu:(RNGridMenu *)gridMenu willDismissWithSelectedItem:(RNGridMenuItem *)item atIndex:(NSInteger)itemIndex {
    
    NSDictionary * dict = [soList objectAtIndex:itemIndex];
    NSString *storyboard=[dict objectForKey:@"StoryBoard"];
    if (![[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        if([storyboard isEqualToString:@"lineChart"]||[storyboard isEqualToString:@"pieChart"])
        {
            storyboard=[[NSString alloc]initWithFormat:@"%@_IPAD",storyboard];
        }
    }
    
    NSString *Identifier=[dict objectForKey:@"Identifier"];
    NSString *ID=[dict objectForKey:@"ID"];
    NSString *ValueFiledName=[dict objectForKey:@"ValueFiledName"];
    NSString *TableFiledName=[dict objectForKey:@"TableFiledName"];
    NSString *DateFiledName=[dict objectForKey:@"DateFiledName"];
    NSString *WhereStr=[dict objectForKey:@"WhereStr"];
    NSString *Title=[dict objectForKey:@"Title"];
    NSString *TableTypeID=[[NSString alloc]initWithFormat:@"%@",[dict objectForKey:@"TableTypeID"]];
    NSString *KeyFiledName=[dict objectForKey:@"KeyFiledName"];
    NSString *KeyFiledName2=[dict objectForKey:@"KeyFiledName2"];
    NSString *TopCount=[[NSString alloc]initWithFormat:@"%@",[dict objectForKey:@"TopCount"]];
    NSString *KeyTableName=[dict objectForKey:@"KeyTableName"];
    if(![storyboard isEqualToString:@""]&&storyboard!=nil){

        UIStoryboard *valueView = [UIStoryboard storyboardWithName:storyboard bundle:nil];
        UIViewController *uv=[valueView instantiateViewControllerWithIdentifier:Identifier];
    
        if([TableTypeID isEqualToString:@"1"]){
            [uv setValue:ID forKey:@"formdid"];//不需要强制转换也可以赋值
            [uv setValue:ValueFiledName forKey:@"ValueFiledName"];//不需要强制转换也可以赋值
            [uv setValue:TableFiledName forKey:@"TableFiledName"];//不需要强制转换也可以赋值
            [uv setValue:DateFiledName forKey:@"DateFiledName"];//不需要强制转换也可以赋值
            [uv setValue:WhereStr forKey:@"WhereStr"];//不需要强制转换也可以赋值
            [uv setValue:Title forKey:@"Title"];//不需要强制转换也可以赋值

        }
        if([TableTypeID isEqualToString:@"2"]){
            [uv setValue:ID forKey:@"formdid"];//不需要强制转换也可以赋值
            [uv setValue:ValueFiledName forKey:@"ValueFiledName"];//不需要强制转换也可以赋值
            [uv setValue:TableFiledName forKey:@"TableFiledName"];//不需要强制转换也可以赋值
            [uv setValue:DateFiledName forKey:@"DateFiledName"];//不需要强制转换也可以赋值
            [uv setValue:WhereStr forKey:@"WhereStr"];//不需要强制转换也可以赋值
            [uv setValue:Title forKey:@"Title"];//不需要强制转换也可以赋值
            
            [uv setValue:KeyFiledName forKey:@"KeyFiledName"];//不需要强制转换也可以赋值
            [uv setValue:KeyFiledName2 forKey:@"KeyFiledName2"];//不需要强制转换也可以赋值
            [uv setValue:TopCount forKey:@"TopCount"];//不需要强制转换也可以赋值
            [uv setValue:KeyTableName forKey:@"KeyTableName"];//不需要强制转换也可以赋值
            
        }
         UINavigationController *baNavigation = [[UINavigationController alloc] initWithRootViewController:uv];
        //[uv setModalTransitionStyle:UIModalTransitionStylePartialCurl];
        
        [self presentModalViewController:baNavigation animated:YES];
        
        //返回
        
        [self dismissModalViewControllerAnimated:YES];
    
        
    
    }
    
   
    
    
}





- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}






@end
